package com.cskaoyan.jdbc.datasource.dbcp;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 创建日期: 2022/05/14 11:08
 *
 * @author ciggar
 */
public class DBCPUtils {

    // 数据库连接池的接口
    static DataSource dataSource;

    static {

        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("dbcp.properties"));

            //  BasicDatasource
            // connection : PoolGuardConnectionWrapper
            dataSource = BasicDataSourceFactory.createDataSource(properties);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    // 获取连接的方法
    public static Connection getConnection(){
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

}
